#!/usr/bin/python
# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [u'GuiTextView']

import sys

from pyLot.core import (check_type, check_keys, hardbook, get_data, StreamWriter)

from _pylot.qtgui.tools import (generate_pyfile_from_uifile, create_background_palette)
from _pylot.qtgui.styles.charformats import PYLOT_CHAR_FORMATS
from _pylot.qtgui.default.impl_widget import Widget

from Qt4 import QtGui


generate_pyfile_from_uifile(__name__,
  src=hardbook(u'uifiles', u'textview.ui'),
  dest=get_data(__name__, u'_gui_textview.py'))

from _gui_textview import Ui_GuiTextView
class GuiTextView(QtGui.QWidget, Ui_GuiTextView, Widget):

  identifier = 'pyLot.widget.gui_textview'
  def __init__(self, formats=PYLOT_CHAR_FORMATS):
    u"""
    :param formats: a dict of QTextCharFormat
    :type formats: obj:`dict`
    """
    QtGui.QWidget.__init__(self)
    Widget.__init__(self)
    self.setupUi(self)

    # TODO: property
    check_type(u'formats', formats, dict)
    for name, fmt in formats.items() :
      check_type(name, fmt, QtGui.QTextCharFormat)
    check_keys(u'formats', formats, [u'std'])
    self.formats = formats

    self.ensure_visible = True

  def write (self, text, style=u'std'):
    try :
      char_format = self.formats[style]
    except KeyError :
      char_format = self.formats[u'std']

    cursor = self.text_edit.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text, char_format)
    cursor.setCharFormat(self.formats[u'std'])
    if self.ensure_visible :
      self.text_edit.moveCursor(QtGui.QTextCursor.End)
      self.text_edit.ensureCursorVisible()

  def clear(self):
    self.text_edit.clear()

  def lockView (self, boolean=True):
    self.ensure_visible = boolean

  def changeBackgroundColor (self, color=QtGui.QColor(255, 255, 255)):
    bg = create_background_palette(color)
    self.text_edit.setPalette(bg)

if __name__ == u"__main__":
  app = QtGui.QApplication(sys.argv)
  win = QtGui.QMainWindow()

  widget = GuiTextView()
  win.setCentralWidget(widget)
  for tool_bar in widget.tool_bars :
    win.addToolBar(tool_bar)
  for menu in widget.menus :
    win.menu_bar.addMenu(menu)

  if widget.pref :
    widget.pref.show()

  widget.write(u'salut\n', u'title')
  widget.write(u'¡ ça marche ! συγχαρητήρια ! 祝賀 !', u'warn')

  win.show()

  app.exec_()

  import doctest
  doctest.testmod()
